package com.wtwd.device.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.wtwd.device.constant.Constants;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.entity.SysRole;
import com.wtwd.device.model.entity.SysUser;
import com.wtwd.device.model.vo.RespCodeEntity;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.SysRoleService;
import com.wtwd.device.utils.GetCurrentSysUserInfoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author mjy
 * @date 2021/08/03
 * @description
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private GetCurrentSysUserInfoUtils getCurrentSysUserInfoUtils;


    /**
     * 获取角色列表
     *
     * @return
     */
    @ApiOperation(value = "获取角色列表")
    @GetMapping("/getAllRoleList")
    public RespEntity getAllRoleList() {
        logger.info("getAllRoleList");
        List<SysRole> sysRoles = sysRoleService.getAllSysRoleList();
        return new RespEntity(RespCode.SUCCESS, sysRoles);
    }

    /**
     * 获取角色权限列表
     *
     * @param roleId
     * @return
     */
    @ApiOperation("获取角色权限列表")
    @DynamicParameters(name = "getRolePermissionList", properties = {
            @DynamicParameter(name = "roleId", value = "角色id", dataTypeClass = Long.class, required = true)
    })
    @GetMapping("/getRolePermissionList/{roleId}")
    public RespEntity getRolePermissionList(@PathVariable Long roleId) {
        SysUser sysUser = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        if (sysUser == null) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        } else {
            SysRole sysRole = sysRoleService.getSysRoleByRoleId(sysUser.getRoleId());
            if (!sysRole.getRoleType().equals(Constants.SYS_USER_ROLE_TYPE_ADMIN)) {
                return new RespEntity(RespCode.NO_PERMISSION);
            }
        }
        return sysRoleService.getRolePermission(sysUser.getRoleId(), roleId);
    }


    /**
     * roleId (角色类型  int)
     * permissionList(菜单集合 Array)
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation("修改角色权限列表")
    @DynamicParameters(name = "updateCustomerModel", properties = {
            @DynamicParameter(name = "roleId", value = "角色id", dataTypeClass = Long.class, required = true)
            ,@DynamicParameter(name = "permissionList", value = "菜单id集合", dataTypeClass = Arrays.class, required = true)
    })
    @PutMapping("/updateRolePermission")
    public RespCodeEntity updateRolePermission(@RequestBody JSONObject jsonObject) {
        logger.info("addRolePermission param:" + jsonObject);
        SysUser sysUser = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        if (sysUser == null) {
            return new RespCodeEntity(RespCode.TOKEN_UNAUTHORIZED);
        } else {
            SysRole sysRole = sysRoleService.getSysRoleByRoleId(sysUser.getRoleId());
            if (!sysRole.getRoleType().equals(Constants.SYS_USER_ROLE_TYPE_ADMIN)) {
                return new RespCodeEntity(RespCode.NO_PERMISSION);
            }
        }
        Long roleId = jsonObject.getLong("roleId");
        List<Integer> permissionList = jsonObject.getJSONArray("permissionList").toJavaList(Integer.class);
        return sysRoleService.addRolePermission(roleId, permissionList);
    }

}
